package com.dxq.test.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: zsh
 * Date: 2023/10/17
 * Describe: ormLite数据库
 */

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String TABLE_NAME = "pasture.db";//数据库名称

    private static int CURRENT_VERSION = 1;//数据库版本

    private static DatabaseHelper instance;

    private Map<String, Dao> daos = new HashMap<>();

    public DatabaseHelper(Context context, String databaseName,
                          SQLiteDatabase.CursorFactory factory,
                          int databaseVersion) {
        super(context, databaseName, factory, databaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, Pasture.class);//创建表
            TableUtils.createTable(connectionSource, Label.class);//创建表
            TableUtils.createTable(connectionSource, Event.class);//创建表
            TableUtils.createTable(connectionSource, Group.class);//创建表
            TableUtils.createTable(connectionSource, Vaccine.class);//创建表
            TableUtils.createTable(connectionSource, Breed.class);//创建表
            TableUtils.createTable(connectionSource, Food.class);//创建表
            TableUtils.createTable(connectionSource, Feed.class);//创建表
            TableUtils.createTable(connectionSource, LabelGroup.class);//创建表
            TableUtils.createTable(connectionSource, LabelEvent.class);//创建表
            TableUtils.createTable(connectionSource, LabelVaccine.class);//创建表
            TableUtils.createTable(connectionSource, GroupVaccine.class);//创建表
            TableUtils.createTable(connectionSource, LabelBreed.class);//创建表
            TableUtils.createTable(connectionSource, FeedGroupFood.class);//创建表
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database,
                          ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            //这里只是粗暴的移除了旧表,新建新表,这样会导致数据丢失,现实一般不这么做
            TableUtils.dropTable(connectionSource, Pasture.class, true);//删除表
            TableUtils.dropTable(connectionSource, Label.class, true);//删除表
            TableUtils.dropTable(connectionSource, Event.class, true);//删除表
            TableUtils.dropTable(connectionSource, Group.class, true);//删除表
            TableUtils.dropTable(connectionSource, Vaccine.class, true);//删除表
            TableUtils.dropTable(connectionSource, Breed.class, true);//删除表
            TableUtils.dropTable(connectionSource, Food.class, true);//删除表
            TableUtils.dropTable(connectionSource, Feed.class, true);//删除表
            TableUtils.dropTable(connectionSource, LabelGroup.class, true);//删除表
            TableUtils.dropTable(connectionSource, LabelEvent.class, true);//删除表
            TableUtils.dropTable(connectionSource, LabelVaccine.class, true);//删除表
            TableUtils.dropTable(connectionSource, GroupVaccine.class, true);//删除表
            TableUtils.dropTable(connectionSource, LabelBreed.class, true);//删除表
            TableUtils.dropTable(connectionSource, FeedGroupFood.class, true);//删除表
            onCreate(database, connectionSource);
            //如果有增加表字段或者减少表字段的时候需要调用一下代码
            //增加减少字段在字段类中
//            if (oldVersion < 2) {
//                DatabaseUtil.upgradeTable(database, connectionSource, Pasture.class, DatabaseUtil.OPERATION_TYPE.DELETE);//减少表字段
//                DatabaseUtil.upgradeTable(database, connectionSource, Label.class, DatabaseUtil.OPERATION_TYPE.ADD);//增加表字段
//            }
//            onCreate(database, connectionSource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized DatabaseHelper getHelper(Context context) {
        if (instance == null) {
            synchronized (DatabaseHelper.class) {
                if (instance == null)
                    instance = new DatabaseHelper(context, TABLE_NAME, null, CURRENT_VERSION);
            }
        }

        return instance;
    }

    //获取dao
    public synchronized Dao getDao(Class clazz) throws SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();
        if (daos.containsKey(className)) {
            dao = daos.get(clazz);
        }
        if (dao == null) {
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }
        return dao;
    }

    //关闭
    @Override
    public void close() {
        super.close();
        for (String key : daos.keySet()) {
            Dao dao = daos.get(key);
            dao = null;
        }
    }

}
